include(['com.unknown.Constants', 'com.unknown.Config'], 
		
function(){
	
	declare('com.unknown.Room');

	com.unknown.Room = function(baseUrl, id, mode){
		this.id = id;
		this.previousId = null;
		this.name = null;
		this.listeners = [];
		this.TYPE_UPDATE = 1;
		this.baseUrl = baseUrl;
		this.mode = mode;
		this.firstLoad = 1;
				
		this.load = function(grid, id, dir){
			var scope = this;
			var url = baseUrl + '/' + this.mode + '/' + id + '/' + dir.row + '/' + dir.col;
			//console.log("Loading: " + grid.name + " in dir ("+dir.col+", " + dir.row+") with id " + id);
            var defer = $.Deferred();
            defer.pipe(function(container, responseText, responseStatus){
                try{
                    if( responseStatus != 'success'){
                        console.log("Failed");
                        console.log(responseText);
                        console.log(responseStatus);
                    }

					var roomId = $('.room_id', container).attr('roomId');
					var name   = $('.room_id', container).attr('roomName');
                    if( !name ){
                        console.log("Name is missing something is wrong");
                    }
					//console.log("Replacing: " + grid.name + " in dir ("+dir.col+", " + dir.row+") with " + name + " " + roomId);
                    grid.roomId = roomId;
					grid.name   = name;
					//grid.container.append("RoomId: "+grid.roomId);
					//grid.container.append("<br/>Name: "+grid.name);
					scope.attachHandlers(grid);
				}catch(ex){
					console.error(ex);
					//alert(ex);
				}
            }).done(function(){
                if( grid.next.length > 0 ){
                    var op = grid.next.pop();
                    var m2d  = grid.node.matrix2d;

                    //console.log("Before: '" + grid.roomId + "' Grid x:" + m2d.x3 + ", y:" + m2d.y3);
                    op();
                    //console.log("After: '"+grid.roomId+"' Grid x:" + m2d.x3 + ", y:" + m2d.y3);
                }else{
                    grid.isDeferred = false;
                }
            });
            grid.container.load(url, function(responseText, responseStatus){
				defer.resolve(this, responseText, responseStatus);
			});
		};
		this.key = function(x, y){
			return "["+x+","+y+"]"
		}
		
		this.attachHandlers = function(grid){
			this.tiles = {}
			var firstcell = null;
			//console.log("Attaching handlers " + grid.name);
			$('img', grid.container).each(function(){
				var left = toNumber(this.style['left']);
				var top  = toNumber(this.style['top']);
				var col = left/35;
				var row = (top/15);
				
				var cell = grid.childAt(row, col);
				if( cell ){
					cell.contents = $(this);
					if(!firstcell){
						//console.log(left+ ", "+top);
						grid.select(cell);
					}
					grid.activeCells.push(cell);
					firstcell = firstcell || cell;
				}else{
					console.log("Cell not found: " + grid.roomId + "(" + grid.name +")" + col +", " + row);
				}
			});
			var t;
			$('a', grid.container).each(function(){
				var left = toNumber(this.style['left']);
				var top  = toNumber(this.style['top']);
				var row = (top/15);
				var col = (left/35);
				
				var cell = grid.childAt(row, col);
				if( !cell || !cell.contents ){
					$(this).hide();
				}else{
					//console.log("Exit: " + cell.x + ", " + cell.y);
					cell.roomid = $(this).attr('roomid');
					cell.name = $(this).attr('title');
				}
				if( $(this).attr('roomid') == room.previousId ){
					firstcell = cell;
				}
			});
			if( firstcell && this.firstLoad ){
				player.x = firstcell.x;
				player.y = firstcell.y+15;
	
				//console.log( "Player @ x="+player.x +', y='+player.y);
				this.firstLoad = 0;
				
			}else{
				
			}
			//if( actionManager )
			//	actionManager.attach();
			//else
			//	this.attach();
		};
		this.addListener = function(type, listener){
			if( !this.listeners[type] ) this.listeners[type] = [];
			this.listeners[type].push(listener);
		};
		this.update = function(name, description){
			this.name = name;
			this.description = description;
			var index = 0;
			var items = this.listeners[this.TYPE_UPDATE];
			for(; index < items.length; index++){
				items[index](name, description);
			}
		};
	
		this.initAction = function(data){
			if( data.action == 'edit' ){
				this.mode = 'edit';
				this.load( this.id );
			}
		};
		this.attach = function(){
			$('.exit').click(function(){
				var id = $(this).attr('roomId');
				room.load(id);
			});
		};
		this.getTile = function(x,y){
			var cell = eGrid.getCell(x,y);
			if( cell && cell.contents)
				return cell;
			return null;
		};
        this.getCell = function(x,y){
            var cell = eGrid.getCell(x,y);
            return cell;
        };
	}
});